﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Security.AccessControl;
using System.Data;

namespace SimpleLibrary.Reports
{
    public enum Reports
    {
        AvailableBooks,
        ExpiredRentals,
        BorrowedBooks
    }
    /// <summary>
    /// Report geneartor based onthe type of report
    /// </summary>
    class ReportWriter
    {
        public static FileStream CreateFile(Reports report)
        {
            if (File.Exists(".\\" + report + ".txt"))
            {
                File.Delete(".\\" + report + ".txt");
            }
            return File.Create(".\\" + report + ".txt", 50, FileOptions.Asynchronous);
        }

        public static void WriteToStream(List<String> table, Reports report,String name)
        {
            TextWriter writer = null;
            try
            {
                writer = new StreamWriter(".\\" + report + ".txt", false, Encoding.Default);
                writer.WriteLine("----------------Report-------------");
                writer.WriteLine("Name of Report : " + name);
                writer.WriteLine("Date Created: " + DateTime.Now);
                writer.WriteLine("-----------------------------------");

                foreach (String data in table)
                {
                    writer.WriteLine(data);
                }
            }
            catch (Exception)
            {

            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }
        }

        internal static void OpenFile(Reports reports)
        {
            if (File.Exists(".\\" + reports + ".txt"))
            {
                Process.Start(".\\" + reports + ".txt");
            }
        }
    }
}
